package com.front.managementfront.region.service.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.front.managementfront.constants.RegionConstants;
import com.front.managementfront.region.entity.HktsRegion;
import com.front.managementfront.region.mapper.HktsRegionMapper;
import com.front.managementfront.region.service.IHktsRegionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hkts.management.common.vo.RedisConstants.REGION;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-12
 */
@Slf4j
@Service
@Transactional
public class HktsRegionServiceImpl extends ServiceImpl<HktsRegionMapper, HktsRegion> implements IHktsRegionService {


    @Autowired
    private HktsRegionMapper hktsRegionMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public IPage<HktsRegion> selectPage(Page<HktsRegion> page, HktsRegion hktsRegion) {
        QueryWrapper<HktsRegion> query = Wrappers.query();
        QueryWrapper<HktsRegion> entityWrapper = getEntityWrapper(query, hktsRegion);
        return hktsRegionMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsRegion> selectList(HktsRegion hktsRegion) {
        QueryWrapper<HktsRegion> query = Wrappers.query();
        QueryWrapper<HktsRegion> entityWrapper = getEntityWrapper(query, hktsRegion);
        return hktsRegionMapper.selectList(entityWrapper);
    }

    @Override
    public List<HktsRegion> selectListByCondition(HktsRegion hegion) {
        List<HktsRegion> list=new ArrayList<HktsRegion>();
        if(!StringUtils.isBlank(hegion.getRegionName())){
            hegion.setRegionType("2");
            QueryWrapper<HktsRegion> query = Wrappers.query();
            QueryWrapper<HktsRegion> entityWrapper = getEntityWrapper(query, hegion);
            List<HktsRegion> region = hktsRegionMapper.selectList(entityWrapper);

            if(region.size()==0){
                return null;
            }
            HktsRegion hktsRegion = region.stream().findFirst().orElse(null);
            HktsRegion rigionCondition=new HktsRegion();
            rigionCondition.setParentCode(hktsRegion.getParentCode());
            QueryWrapper<HktsRegion> queryNext = Wrappers.query();
            List<HktsRegion> hktsRegions = hktsRegionMapper.selectList(getEntityWrapper(queryNext, rigionCondition));
            hktsRegions.stream().forEach(regions->{
                if(regions.getRegionCode().equals(hktsRegion.getRegionCode())){
                    HktsRegion rigionConditionNext=new HktsRegion();
                    rigionConditionNext.setParentCode(hktsRegion.getRegionCode());
                    QueryWrapper<HktsRegion> queryN = Wrappers.query();
                    List<HktsRegion> hktsRegionNext = hktsRegionMapper.selectList(getEntityWrapper(queryN, rigionConditionNext));
                    regions.setNextRegions(hktsRegionNext);
                }
            });
            list.addAll(hktsRegions);
        }else{
            QueryWrapper<HktsRegion> query = Wrappers.query();
            if(hegion.getParentCode()==null){
                hegion.setRegionType("1");
            }
            list = hktsRegionMapper.selectList(getEntityWrapper(query,hegion));
        }
        return list;
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsRegion> getEntityWrapper(QueryWrapper<HktsRegion> entityWrapper, HktsRegion hktsRegion) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsRegion.getCreateBy())) {
            entityWrapper.like(HktsRegion.CREATE_BY, hktsRegion.getCreateBy());
        }
        if (StringUtils.isNotBlank(hktsRegion.getUpdateBy())) {
            entityWrapper.like(HktsRegion.UPDATE_BY, hktsRegion.getUpdateBy());
        }
        if (hktsRegion.getParentCode() != null) {
            entityWrapper.eq(HktsRegion.PARENT_CODE, hktsRegion.getParentCode());
        }
        if (StringUtils.isNotBlank(hktsRegion.getRegionName())) {
            entityWrapper.like(HktsRegion.REGION_NAME, hktsRegion.getRegionName());
        }
        if (StringUtils.isNotBlank(hktsRegion.getRegionType())) {
            entityWrapper.like(HktsRegion.REGION_TYPE, hktsRegion.getRegionType());
        }
        if (StringUtils.isNotBlank(hktsRegion.getRegionAlias())) {
            entityWrapper.like(HktsRegion.REGION_ALIAS, hktsRegion.getRegionAlias());
        }
        if (hktsRegion.getRegionCode() != null && hktsRegion.getRegionCode()!=0) {
            entityWrapper.eq(HktsRegion.REGION_CODE, hktsRegion.getRegionCode());
        }
        return entityWrapper;
    }

    @Override
    public HktsRegion getRegionByCode( String regionCode) {
        QueryWrapper<HktsRegion> query = Wrappers.query();
        query.lambda().eq(HktsRegion::getRegionCode,Integer.parseInt(regionCode));
        HktsRegion hktsRegion = hktsRegionMapper.selectOne(query);
        return hktsRegion;
    }


    @Override
    public String getPrentName( String regionCode) {
        QueryWrapper<HktsRegion> query = Wrappers.query();
        query.lambda().eq(!"".equals(regionCode),HktsRegion::getRegionCode,Integer.parseInt(regionCode));
        HktsRegion hktsRegion = hktsRegionMapper.selectOne(query);
        String str = "";

        String prentCode = getPrentCode(hktsRegion.getParentCode());
        str = prentCode + " " + hktsRegion.getRegionAlias();

        return str;
    }
    public String getPrentCode(Integer prentCode){
        QueryWrapper<HktsRegion> queryPrent = Wrappers.query();
        queryPrent.lambda().eq(prentCode != null,HktsRegion::getRegionCode,prentCode);
        HktsRegion hktsRegion = hktsRegionMapper.selectOne(queryPrent);

        String str = "";

        if (hktsRegion != null){
            String prentCode1 = getPrentCode(hktsRegion.getParentCode());
            str = prentCode1 + " " + hktsRegion.getRegionAlias();
        }
        return str;
    }

    public String getPrentRegionCode( String regionCode) {
        QueryWrapper<HktsRegion> query = Wrappers.query();
        query.lambda().eq(!"".equals(regionCode),HktsRegion::getRegionCode,Integer.parseInt(regionCode));
        HktsRegion hktsRegion = hktsRegionMapper.selectOne(query);
        String str = "";

        String prentCode = getPrentCodeT(hktsRegion.getParentCode());
        str = prentCode + " " + hktsRegion.getRegionCode();

        return str;
    }
    public String getPrentCodeT(Integer prentCode){
        QueryWrapper<HktsRegion> queryPrent = Wrappers.query();
        queryPrent.lambda().eq(prentCode != null,HktsRegion::getRegionCode,prentCode);
        HktsRegion hktsRegion = hktsRegionMapper.selectOne(queryPrent);

        String str = "";

        if (hktsRegion != null){
            String prentCode1 = getPrentCodeT(hktsRegion.getParentCode());
            str = prentCode1 + " " + hktsRegion.getRegionCode();
        }
        return str;
    }

    @Override
    public String getRegionRedisByCode(int code) {
        String result= redisTemplate.opsForValue().get(REGION+code);
        if(result==null){
            Map<String, Object> region= hktsRegionMapper.getRegionByCode(code);
            redisParamSave(region);
        }
        result= redisTemplate.opsForValue().get(REGION+code);
        return result;
    }

    private void redisParamSave(Map<String, Object> item){
        Map<String, String> region = new HashMap<>(5);
        String regionCode = item.get(RegionConstants.REGION_CODE).toString();
        region.put(RegionConstants.REGION_CODE, regionCode);
        region.put(RegionConstants.REGION_TYPE, item.get(RegionConstants.REGION_TYPE).toString());
        Integer regionType = Integer.valueOf(item.get(RegionConstants.REGION_TYPE).toString());
        switch (regionType) {
            case 1:
                region.put(RegionConstants.PROVINCE, item.get("region3").toString());
                region.put(RegionConstants.CITY, "");
                region.put(RegionConstants.COUNTY, "");
                break;
            case 2:
                region.put(RegionConstants.PROVINCE, item.get("region2").toString());
                region.put(RegionConstants.CITY, item.get("region3").toString());
                region.put(RegionConstants.COUNTY, "");
                break;
            case 3:
                region.put(RegionConstants.PROVINCE, item.get("region1").toString());
                region.put(RegionConstants.CITY, item.get("region2").toString());
                region.put(RegionConstants.COUNTY, item.get("region3").toString());
                break;
        }
        String param = JSON.toJSONString(region);
        log.info("组装的数据：{}", param);
        redisTemplate.opsForValue().set(REGION + regionCode, param);
    }
}
